package ua.sumdu.LukyanovChalyi.Server;

import java.util.Map;
import java.util.HashMap;

public class Model {

    private Map<Long, CopyBook> books;

    public Model() {
        books = new HashMap<Long, CopyBook>();
    }

    public synchronized void addCopyBook(CopyBook copyBook) {
        Long id = new Long (copyBook.getID());
        if (!books.containsKey(id)) {
            books.put(id, copyBook);
        }
    }

    public synchronized void removeCopyBookByID(long id) throws ModelException {
        Long key = new Long(id);
        if (books.keySet().contains(key)) {
            books.remove(key);
        } else {
            throw new ModelException("There isn't book with this id");
        }
    }

    public synchronized void setActive(long id, boolean active) throws ModelException {
        CopyBook copyBook = books.get(id);
        if (copyBook != null) {
            copyBook.setActive(active);
        } else {
            throw new ModelException("There isn't book with this id");
        }
    }

    public synchronized void addAllBooks(Map<Long, CopyBook> map) {
        books.putAll(map);
    }

    public synchronized Map<Long, CopyBook> getBooks() {
        return books;
    }
}